package ext.tianma.part.statute;

import java.util.List;

import org.apache.log4j.Logger;
import ext.generic.erp.common.CommonPDMUtil;
import ext.tianma.util.IbaAttributeUtil;
import ext.tianma.util.TianMaConstants;
import wt.enterprise.MadeFromLink;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.WTObject;
import wt.log4j.LogR;
import wt.part.WTPart;

/**
 * 用于判断进阶和二供流程中,用户修改物料时,环保类型的变化
 * 
 * @author laichao_sun
 *
 */

public class StatuteVersionUpadte {
	
	private static final Logger log = LogR.getLogger(StatuteVersionUpadte.class.getName());
	
	public static boolean isEPCChange(WTObject object) throws Exception {
		// 得到当前部件的环保等级
		String partEnvProtectionCategory2 = "";
		WTPart part2 = null;
		if (object instanceof WTPart) {
			part2 = (WTPart) object;
			partEnvProtectionCategory2 = IbaAttributeUtil.getAttributeValue(part2, "partEnvProtectionCategory");
		}
		//判断生效地是否在深圳
		boolean flag = checkEffectiveSite(part2);
		if (flag) {
			
		
		/**
		 * 获取关联的部件的环保等级
		 */
		String partEnvProtectionCategory = "";

		// 获取当前部件的关联的部件
		WTPart part = null;
		WTPart designPart = CommonPDMUtil.getWTPartByNumViewAndVersion(part2.getNumber(), "Design", "0.0");
		QueryResult qr = PersistenceHelper.manager.navigate(designPart, "original", MadeFromLink.class, false);
		if ((qr == null) || (qr.size() == 0)) {
			log.info("深圳法规审核人员验证,没有拿到关联的部件!");
			return false;
		} else {
			while (qr.hasMoreElements()) {
				MadeFromLink link = (MadeFromLink) qr.nextElement();

				part = (WTPart) link.getOriginal();
			}
			partEnvProtectionCategory = IbaAttributeUtil.getAttributeValue(part, "partEnvProtectionCategory");
			// 判断两次环保等级是否一致
			//关联物料的环保等级为RoHS、B级、Q/S0003,新物料为RoHS 新物料不走法规
			//定义一个旧的环保类别字符串
			if(partEnvProtectionCategory.contains("/")) {
				partEnvProtectionCategory = partEnvProtectionCategory.replace("/", "");
			}
			if (partEnvProtectionCategory.contains("+")) {
				partEnvProtectionCategory = partEnvProtectionCategory.replace("+", "");
			}
			String oldPEPC = "RoHSBQ/S0003QS0003";
			if(oldPEPC.contains(partEnvProtectionCategory)&&partEnvProtectionCategory2.equals("RoHS")) {
				return false;
			}
			//System.out.println("slc===partEnvProtectionCategory==="+partEnvProtectionCategory);
			//System.out.println("slc===partEnvProtectionCategory2=="+partEnvProtectionCategory2);
			if (partEnvProtectionCategory.equals(partEnvProtectionCategory2)) {
				return false;
			} else {
				return true;
			}
		}
		}else {
			return false;
		}
	}

	// 检验料号生效地是否有产地C01/02、04、08
	private static boolean checkEffectiveSite(WTPart part) {
		List<String> effectiveSite = IbaAttributeUtil.getAttributeValues(part, "partNumberEffectiveSite");
		boolean temp = false;
		for (String site : effectiveSite) {
			if (site.contains(TianMaConstants.EffectiveSite.C01.getSite())||site.contains(TianMaConstants.EffectiveSite.C02.getSite())||site.contains(TianMaConstants.EffectiveSite.C04.getSite())||site.contains(TianMaConstants.EffectiveSite.C08.getSite())) {
				temp = true;
				break;
			}
		}
		return temp;
	}
}